<?php
/**
 * Created by PhpStorm.
 * User: THINK
 * Date: 2017/8/24
 * Time: 19:34
 */

namespace Admin\Controller;



use Think\Controller;
use Org\Util\Rbac;

class UserController extends Controller
{
    public function login(){
        if (isset($_POST['sub'])){
            $user = M('User');
            $username = I('username');
            $password = I('password');
            $checkcode = I('code');
            $data['username'] = $username;
            $result = $user->where($data)->find();
            $verify = new \Think\Verify();
            $msa=$verify->check($checkcode);
            if ($msa) {
                if (md5(md5($password)) == $result['password']) {
                    session(C('USER_AUTH_KEY'), $result['id']);//将用户id设置成用户识别号
                    session('username',$result['username']);
                    //if ($_SESSION['username'] == C('RBAC_SUPERADMIN')) session(C('ADMIN_AUTH_KEY'), true);
                    //用于检测用户权限的方法,并保存到Session中,读取用户权限
                    //Rcba::saveAccessList($result['id']);
                    $result['login_ip']=get_client_ip();
                    $result['time']=time();

                    if($user->save($result)){
                        $this->redirect('Index/index', array('cate_id' => 2), 1, '页面跳转中...');

                    }
                    $this->redirect('User/login', array('cate_id' => 2), 1, '失败...');

                }else{
                    echo "<script language=\"JavaScript\">alert('用户名或密码错误');</script>";
                }
            }else{
                echo "<script language=\"JavaScript\">alert('验证码错误');</script>";
                $this->display();
            }
        }else{
            $this->display();
        }

//                    if($result['username']=='admin'){
//                        session(C('ADMIN_AUTH_KEY'),true);
//                    }
//                    \Org\Util\Rbac::saveAccessList();

    }
    public function logout(){
        session('username',null);
        $this->redirect('User/login','',1,'退出成功，跳转登录页');
    }
    public function verify(){
        //ob_clean();
        $Verify = new \Think\Verify();
        $Verify->fontSize = 30;
        $Verify->length   = 4;
        $Verify->useNoise = false;
        $Verify->entry();
    }

}